package com.lagou.ModelTest3.First.Two;

import java.util.Objects;

public class Worker {
    //包含三个私有属性：姓名(String name)、年龄(int age)、工资(double salary
    private String name;
    private int age;
    private double salary;
    private Object obj;
    //空参有参构造
    public Worker(){}
    public Worker(String name,int age,double salary){
        setName(name);
        setAge(age);
        setSalary(salary);
    }

    public Worker(String name) {
        this.name = name;
    }
    public Worker(Object obj){
        this.obj=obj;
    }
    //set、get方法
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name=name;
    }
    public int getAge(){
        return age;
    }
    public void setAge(int age){
        if(age>0&&age<100){
            this.age=age;
        }else{
            System.out.println("年龄不合理哦!");
        }
    }
    public double getSalary(){
        return salary;
    }
    public void setSalary(double salary){
        if(salary>0){
            this.salary=salary;
        }else{
            System.out.println("工资不合理哦!");
        }
    }

    public Object getObj() {
        return obj;
    }

    public void setObj(Object obj) {
        this.obj = obj;
    }

    //equals方法、hashCode方法
    //为Worker类重写equals 方法，当姓名、年龄、工资全部相等时候才返回true
    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Worker){
            Worker wo=(Worker) obj;
            return this.getName()==wo.getName()&&this.getAge()==wo.getAge()&&this.getSalary()==wo.getSalary();
        }else{
            return false;
        }
    }
    @Override
    public int hashCode() {
        return Objects.hash(name, age, salary);
    }
    //toString方法
    @Override
    public String toString() {
        return "Worker{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }
    //Worker类中定义work方法打印一句话，如："凤姐 正在工作"，其中"凤姐"代表当前对象的姓名
    public void work(){
        System.out.println(getObj()+"正在工作");
    }
}
